//
//  SMPlanView.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/10/15.
//

import UIKit
import SnapKit

class SMPlanView: UIView {
    // 套餐数据与点击回调
    var title: String
    var price: String
    var subPrice: String
    var isPopular: Bool
    var tapHandler: (() -> Void)?
    
    // UI 组件
    private let titleLabel = UILabel()
    private let priceLabel = UILabel()
    private let subPriceLabel = UILabel()
    private let mostPopularBadge = UILabel()
    
    // 边框图层
    private let borderLayer = CAShapeLayer()

    // 初始化
    init(title: String, price: String, subPrice: String, isPopular: Bool) {
        self.title = title
        self.price = price
        self.subPrice = subPrice
        self.isPopular = isPopular
        super.init(frame: .zero)
        setupUI()
        setupGestures()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置 UI
    private func setupUI() {
        backgroundColor = UIColor.clear
        
        // 设置边框图层
        borderLayer.strokeColor = UIColor(hex: "#FFFFFF", alpha: 0.16).cgColor
        borderLayer.fillColor = UIColor.clear.cgColor
        borderLayer.lineWidth = 1.0
        layer.addSublayer(borderLayer)
        
        // 标题
        titleLabel.text = title
        titleLabel.textColor = .white
        titleLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        addSubview(titleLabel)
        
        // 价格
        priceLabel.text = price
        priceLabel.textColor = UIColor(hex: "#FFFFFF")
        priceLabel.font = UIFont.systemFont(ofSize: 16)
        addSubview(priceLabel)
        
        // 子价格（如 "每周仅 $0.69"）
        subPriceLabel.text = subPrice
        subPriceLabel.textColor = UIColor(hex: "#FFFFFF", alpha: 0.6)
        subPriceLabel.font = UIFont.systemFont(ofSize: 16)
        addSubview(subPriceLabel)
        
        // 热门标签（仅热门套餐显示）- 最后添加确保在最上层
        if isPopular {
            mostPopularBadge.text = "MOST POPULAR"
            mostPopularBadge.textColor = .white
            mostPopularBadge.font = UIFont.systemFont(ofSize: 10)
            mostPopularBadge.backgroundColor = UIColor(hex: "#1F87F6")
            mostPopularBadge.layer.cornerRadius = 10
            mostPopularBadge.clipsToBounds = true
            mostPopularBadge.textAlignment = .center
            // 确保 badge 显示在最上层
            addSubview(mostPopularBadge)
            bringSubviewToFront(mostPopularBadge)
        }
        
        // SnapKit 约束内部子视图
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
        }
        
        priceLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.top.equalToSuperview().offset(8)
           
            if subPrice.isEmpty {
                make.bottom.equalToSuperview().offset(-16)
            } else {
                make.width.equalTo(subPriceLabel)
            }
        }
        
        if !subPrice.isEmpty {
            subPriceLabel.snp.makeConstraints { make in
                make.trailing.equalToSuperview().offset(-16)
                make.bottom.equalToSuperview().offset(-8)
            }
        }

        if isPopular {
            mostPopularBadge.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(10)
                make.top.equalToSuperview().offset(-10)
                make.width.equalTo(105)
                make.height.equalTo(20)
            }
        }
    }
    
    // 在布局完成后设置圆角为半圆
    override func layoutSubviews() {
        super.layoutSubviews()
        let cornerRadius = bounds.height / 2
        
        // 更新边框路径
        let path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
        borderLayer.path = path.cgPath
        
        // 确保 badge 在最上层
        if isPopular {
            bringSubviewToFront(mostPopularBadge)
        }
    }
    
    // 点击手势
    private func setupGestures() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        addGestureRecognizer(tap)
        isUserInteractionEnabled = true
    }
    
    @objc private func handleTap() {
        tapHandler?()
    }
    
    // 添加选中状态管理
    func setSelected(_ selected: Bool) {
        if selected {
            borderLayer.strokeColor = UIColor(hex: "#1F87F6").cgColor
        } else {
            borderLayer.strokeColor = UIColor(hex: "#FFFFFF", alpha: 0.16).cgColor
        }
    }
    
    func resetSelection() {
        setSelected(false)
    }
}
